//
//  CountDownTask.m
//  HZBus
//
//  Created by user on 16/10/28.
//  Copyright © 2016年 user. All rights reserved.
//

#import "CountDownTask.h"

@implementation CountDownTask

- (void)main {// main结束,一般就标志着这个队列该自动移除NSOperationQueue
    self.taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
    
    while (-- (_leftTimeInterval) > 0) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_countingDownBlock) {
                _countingDownBlock(_leftTimeInterval);
            }
        });
        //线程睡眠一秒钟
        [NSThread sleepForTimeInterval:1];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (_finishedBlock) {
            _finishedBlock(0);
        }
    });
    
    if (self.taskIdentifier != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:self.taskIdentifier];
        self.taskIdentifier = UIBackgroundTaskInvalid;
    }
    
    
}

@end
